import { Player } from '@/core/playersystem/player';
import { BasicCard, Card, Color, HurtType, CardType } from './card';
import { Sha, Shan, Jiu, Tao } from './basicCard'
import EQ from './equipments/index';
import BR from './brilliantness/index'
import GLOBAL_CONFIG from '../gamesystem/gameconfig'
import _ from 'lodash'

export class CardsHeap {

    // 所有牌
    allCards: Card[]

    // 在用的牌
    inuseCards: Card[]

    // 已使用中的牌
    usedCards: Card[]


    constructor() {
        this.allCards = []
        this.inuseCards = []
        this.usedCards = []
        this.initCards()
    }


    createCard<T extends Card>(target: CardType<T>, ...args: any[]): T {
        return new target(...args)
    }

    initCards(): void {
        this.initBasicCards()
        this.initEquipment()
        this.initStrategyCards()
        this.inuseCards = _.shuffle(this.allCards)
        
    }

    shuffleCard() {
        if (this.usedCards.length > 0) {
            this.inuseCards = this.inuseCards.concat(_.shuffle(this.usedCards))
            this.usedCards = []
        }
    }

    initBasicCards(): void {
        // 杀
        this.addCards(Sha, Color.HEART, 10, 10, 11)
        this.addCards(Sha, Color.DIAMOND, 6, 7, 8, 9, 10, 13)
        this.addCards(Sha, Color.SPADE, 7, 8, 8, 9, 9, 10, 10)
        this.addCards(Sha, Color.CLUB, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11)

        this.addCards2(Sha, Color.SPADE, HurtType.THUNDER, 4, 5, 6, 7, 8)
        this.addCards2(Sha, Color.CLUB, HurtType.THUNDER, 5, 6, 7, 8)
        this.addCards2(Sha, Color.DIAMOND, HurtType.FIRE, 4, 5)
        this.addCards2(Sha, Color.HEART, HurtType.FIRE, 4, 7, 10)

        // 闪
        this.addCards(Shan, Color.HEART, 2, 2, 8, 9, 10, 11, 12, 13)
        this.addCards(Shan, Color.DIAMOND, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 11, 11)

        this.addCards(Tao, Color.HEART, 3, 4, 5, 6, 7, 8, 9, 12)
        this.addCards(Tao, Color.DIAMOND,  2, 2, 3, 12)

        this.addCards(Jiu, Color.CLUB, 3,9)
        this.addCards(Jiu, Color.SPADE, 3,9)
        this.addCards(Jiu, Color.DIAMOND, 9)

    }

    initEquipment(): void {
        for (const creator of EQ.EquipmentCardCreator.values()) {
            const card = creator()
            this.allCards.push(card)
        }
    }

    initStrategyCards(): void {
        for (const creator of BR.BrilliantCardCreatorMap.values()) {
            const cards = creator()
            cards.forEach((item) => this.allCards.push(item))
        }
    }

    addCards<T extends Card>(target: CardType<T>, color: Color, ...numbers: number[]): void {
        for (const value of numbers) {
            const args: any[] = [color, value]
            const card: Card = this.createCard(target, ...args)
            this.allCards.push(card)
        }
    }

    addCards2<T extends Card>(target: CardType<T>, color: Color, hurtType: HurtType, ...numbers: number[]): void {
        for (const value of numbers) {
            const args: any[] = [color, value, hurtType]
            const card: Card = this.createCard(target, ...args)
            this.allCards.push(card)
        }
    }

    drawACard(): Card | undefined {
        if (this.inuseCards.length < 2) {
            this.shuffleCard()
        }
        return this.inuseCards.shift()
    }

    drawCards(num: number): Card[] {
        if (this.inuseCards.length < num) {
            this.shuffleCard()
        }
        return this.inuseCards.splice(0, num)
    }

    returnCard(card: Card): void {
        this.usedCards.push(card)
    }

}




